package com.duapk.web.listener;

import java.io.File;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.duapk.web.conf.SystemConfig;
import com.duapk.web.log.LogManager;



/**
 * web应用启动加载
 * @author yangfeng
 *
 */
public class SystemInitListener implements ServletContextListener{

	@Override
	public void contextDestroyed(ServletContextEvent contextEvent) {
		//干掉
	}

	@Override
	public void contextInitialized(ServletContextEvent contextEvent) {
		String configPath = System.getProperty("conf");
		if(configPath==null){
			System.out.println("java vm parameters not found!, please add -Dconf=xxx to VM parameters!");
			System.exit(-1);
		}
		String systemName = contextEvent.getServletContext().getInitParameter("systemName");
		configPath = configPath + File.separator + systemName;
		File file = new File(configPath);
		if(file.exists() && file.isDirectory()){
			systemLoad(configPath);
		}else{
			System.out.println("You provided ConfigPath error!");
			System.exit(-1);
		}
	}
	
	private void systemLoad(String configPath){
		System.out.println("Start get Log Config File ... ");
		String logConfigFile = configPath + File.separator
				+ "logback.xml";
		LogManager.init(logConfigFile);
		System.out.println("Finished Load Log Configuraton!");

		System.out.println("Start get System Config File ... ");
		String systemConfigFile = configPath + File.separator
				+ "SystemConfig.xml";
		SystemConfig.load(systemConfigFile);
		System.out.println("Finished Load System Configuraton!");
	}
	
}
